package com.runner.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class LoadingAssetsScreen implements Screen, Constants {

	private boolean render;
	private BitmapFont font, titleFont, creditFont;
	private SpriteBatch batch;
	private ShapeRenderer loadBar;
	private ShapeRenderer loadBarOutline;
	private SpriteBatch uiBatch;
	private Image background;
	private Label heading;
	private Label credits;
	private Stage stage;
	private int random;

	public static final float X_POS = 0.08f;

	public LoadingAssetsScreen() {
		batch = new SpriteBatch();
		render = true;
		uiBatch = new SpriteBatch();
		background = new Image(new Texture(BACKGROUNDMAIN_GEO));
		loadBar = new ShapeRenderer();
		loadBarOutline = new ShapeRenderer();
		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(FONT));
		FreeTypeFontParameter param = new FreeTypeFontParameter();
		param.size = (int) (54);
		font = generator.generateFont(param);
		param.size = (int) (96);
		titleFont = generator.generateFont(param);
		param.size = (int) (40);
		creditFont = generator.generateFont(param);
		generator.dispose();

		stage = new Stage(Locator.getViewport());
		// create title
		LabelStyle headingStyle = new LabelStyle(titleFont, Color.DARK_GRAY);
		heading = new Label("Blockland Runner", headingStyle);

		credits = new Label("Developed by Chris Johnson\nThanks to www . kenney . nl for the art assets", new LabelStyle(creditFont, Color.DARK_GRAY));

		credits.setPosition(ACTUAL_VIRTUAL_WIDTH * X_POS, ACTUAL_VIRTUAL_HEIGHT * 0.65f);

		heading.setPosition(ACTUAL_VIRTUAL_WIDTH * X_POS, ACTUAL_VIRTUAL_HEIGHT * 0.80f);
		stage.addActor(background);
		stage.addActor(heading);
		stage.addActor(credits);
		random = MathUtils.random(0, Constants.tips.size() - 1);
		
		loadBarOutline.setTransformMatrix(stage.getCamera().view);
		loadBar.setTransformMatrix(stage.getCamera().view);
		batch.setTransformMatrix(stage.getCamera().view);
		uiBatch.setTransformMatrix(stage.getCamera().view);
		loadBarOutline.setProjectionMatrix(stage.getCamera().projection);
		loadBar.setProjectionMatrix(stage.getCamera().projection);
		batch.setProjectionMatrix(stage.getCamera().projection);
		uiBatch.setProjectionMatrix(stage.getCamera().projection);

	}

	@Override
	public void render(float delta) {
		font.setColor(Color.DARK_GRAY);
		
		if (render && Locator.getAssets().update()) {
			Locator.getGame().init(); // init the starting screens with the
										// assets
			render = false;
		}
		stage.act(delta);
		stage.draw();
		
		uiBatch.begin();
		font.drawWrapped(uiBatch, "Tip: " + Constants.tips.get(random), ACTUAL_VIRTUAL_WIDTH * X_POS, ACTUAL_VIRTUAL_HEIGHT * 0.3f,
				ACTUAL_VIRTUAL_WIDTH * (1 - X_POS));
		uiBatch.end();
		renderLoadBar();
		if (render) {
			batch.begin();
			String s = String.valueOf((int) (Locator.getAssets().getProgress() * 100)) + " %";
			font.draw(batch, "Loading " + s, ACTUAL_VIRTUAL_WIDTH * (X_POS + 0.30f), ACTUAL_VIRTUAL_HEIGHT * 0.12f);
			batch.end();
		}
	}

	private void renderLoadBar() {
		loadBarOutline.begin(ShapeType.Line);
		loadBarOutline.rect(ACTUAL_VIRTUAL_WIDTH * X_POS, ACTUAL_VIRTUAL_HEIGHT * 0.05f, ACTUAL_VIRTUAL_WIDTH * (1 - 2 * X_POS),
				ACTUAL_VIRTUAL_HEIGHT * 0.10f);
		loadBarOutline.end();

		loadBar.begin(ShapeType.Filled);
		loadBar.rect(ACTUAL_VIRTUAL_WIDTH * X_POS, ACTUAL_VIRTUAL_HEIGHT * 0.05f, ACTUAL_VIRTUAL_WIDTH * (1 - 2 * X_POS)
				* Locator.getAssets().getProgress(), ACTUAL_VIRTUAL_HEIGHT * 0.10f);
		loadBar.end();

	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
